package com.dq.ad;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.dq.ad.dialog.BaseAdDialogLoading;
import com.hx.currency.HXCurrencySDK;
import com.qq.e.o.HXADConfig;
import com.qq.e.o.ads.v2.HXSdk;
import com.qq.e.o.ads.v2.ads.banner.BannerAD;
import com.qq.e.o.ads.v2.ads.banner.BannerADListener;
import com.qq.e.o.ads.v2.ads.interstitial.InterstitialAD;
import com.qq.e.o.ads.v2.ads.nativ.NativeAD;
import com.qq.e.o.ads.v2.ads.nativ.NativeADListener;
import com.qq.e.o.ads.v2.ads.splash.SplashAD;
import com.qq.e.o.ads.v2.ads.splash.SplashADListener;
import com.qq.e.o.ads.v2.ads.video.FullscreenVideoAD;
import com.qq.e.o.ads.v2.ads.video.FullscreenVideoADListener;
import com.qq.e.o.ads.v2.ads.video.RewardVideoAD;
import com.qq.e.o.ads.v2.ads.video.RewardVideoADListener;
import com.qq.e.o.ads.v2.error.AdError;
import com.qq.e.o.data.model.AdLogInfo;
import com.qq.e.o.data.model.RewardInfo;
import com.qq.e.o.utils.DisplayUtil;
import com.qq.e.o.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

public class AdManager {

    private static final String TAG = "AdManager";
    public static volatile String channel;

    private BannerAD bad;
    private NativeAD nad;
    private InterstitialAD iad;
    private FullscreenVideoAD fvAd;
    private RewardVideoAD rvAd;
    private OnFullscreenVideoADListener fvAdListener;
    private OnRewardVideoADListener rvAdListener;

    private boolean isRewardClosed = false;//激励视频是否点了关闭
    private boolean isRewardSendSus = false;//激励视频是否上报成功
    private boolean isRewardCallBacked = false;//激励视频是否已经回调onClose()

    public static int NATIVE_AD_TYPE_UP_IMAGE = HXADConfig.NATIVE_AD_TYPE.UP_IMAGE;

    private static final class AdManagerHolder {
        static final AdManager INSTANCE = new AdManager();
    }

    public static AdManager getInstance() {
        return AdManagerHolder.INSTANCE;
    }

    public void useTextureView(boolean use) {
        HXADConfig.useTextureView(use);
    }

    public void supportMultiProcess(boolean support) {
        HXADConfig.supportMultiProcess(support);
    }

    public void initSDK(Context context, String appId, String chId, String cpId, boolean isDebug, boolean isLog) {
        initSDK(context, appId, chId, cpId, isDebug, isLog, true);
    }

    /**
     * 初始化广告SDK
     *
     * @param context context
     * @param appId   hxAppId
     * @param chId    渠道号
     * @param cpId    CP号
     * @param isDebug 是否测试服广告 默认值false
     * @param isLog   是否打印log 建议BuildConfig.DEBUG
     * @param isCrash 是否拦截崩溃异常 默认值true
     */
    public void initSDK(final Context context, final String appId, final String chId, final String cpId, final boolean isDebug, final boolean isLog, final boolean isCrash) {
        channel = chId;

        HXADConfig.setDEBUG(isDebug);
        HXADConfig.setLogger(isLog);
        Log.e(TAG, "hxAppId: " + appId + ", chId: " + channel + ", cpId: " + cpId + ", isLog: " + isLog + ", isDebug: " + isDebug);
        HXSdk.initSDK(context, appId, channel, cpId, isCrash);
    }

    /**
     * 显示原生广告
     */
    public void showNativeAD(Activity activity, ViewGroup viewGroup) {
        if (nad.isPreloaded())
            nad.showAD(activity, viewGroup);
        else
            nad.loadAD();
    }

    public void showFullscreenVideoAD(Activity activity) {
        showFullscreenVideoAD(activity, null);
    }

    /**
     * 显示全屏视频广告
     */
    public void showFullscreenVideoAD(Activity activity, OnFullscreenVideoADListener listener) {
        if (fvAd.isPreloaded()) {
            if (listener != null) {
                fvAdListener = listener;
            }
            fvAd.showAD(activity);
        } else {
            loadFullscreenVideoAD(activity, false, listener);
        }
    }

    public void showRewardVideoAD(String rewardName, Activity activity) {
        rvAdListener = null;
        showRewardVideoAD(rewardName, "-1", activity, null);
    }

    public void showRewardVideoAD(String rewardName, Activity activity, OnRewardVideoADListener listener) {
        showRewardVideoAD(rewardName, "-1", activity, listener);
    }

    /**
     * 显示激励视频广告
     */
    public void showRewardVideoAD(String rewardName, String rewardAmount, Activity activity, OnRewardVideoADListener listener) {
        Log.i(TAG, "showRewardVideoAD");
        isRewardClosed = false;
        isRewardCallBacked = false;
        isRewardSendSus = false;
        rvAdListener = null;

        Map<String, String> map = new HashMap<>();
        map.put("user_id", HXCurrencySDK.getInstance().getUserId());
        map.put("reward_name", rewardName);
        map.put("reward_amount", rewardAmount);
        map.put("reward_extra", "1");  //1:广告校验，0：广告不校验
        getRvAd().setRewardParams(rewardAmount.equals("-1") ? null : map);

        showLoading(activity);
        if (rvAd != null && rvAd.isPreloaded()) {
            if (listener != null) {
                rvAdListener = listener;
            }
            Log.e(TAG, "预加载成功，直接显示激励视频");
            rvAd.showAD(activity);
        } else {
            Log.e(TAG, "预加载失败，直接加载激励视频");
            loadRewardVideoAD(activity, false, listener);
        }
    }

    private BaseAdDialogLoading dialogLoading;
    private Handler handler;

    private void showLoading(final Activity activity) {
        Log.e(TAG, "广告正在加载，展示广告Loading");

        if (activity.isFinishing()) {
            return;
        }

        if (dialogLoading == null) {
            dialogLoading = new BaseAdDialogLoading(activity);
        }

        dialogLoading.show();

        if (handler == null) {
            handler = new Handler();
        }

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "超时关闭Loading");
//                if (rvAd != null) {
//                    rvAd.destroy();
//                    rvAd = null;
//                }
//                if (rvAdListener != null) {
//                    rvAdListener = null;
//                }
                closeLoading("加载广告超时，请切换网络再试", activity);
            }
        }, 8000);
    }

    private void closeLoading(String value, Activity activity) {
        if (!TextUtils.isEmpty(value)) {
            ToastUtil.showShort(activity, value);
        }

        if (dialogLoading != null && !activity.isFinishing()) {
            dialogLoading.dismiss();
            dialogLoading = null;
        }
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

    /**
     * 预加载视频广告
     *
     * @param activity a
     */
    public void preloadVideoAd(final Activity activity) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
//                loadFullscreenVideoAD(activity, true);
                loadRewardVideoAD(activity, true);
            }
        });
    }

    public boolean getHxAdOpen() {
        return HXADConfig.isAdOpen();
    }

    /**
     * 加载开屏广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adListener  监听回调
     */
    public void showSplashAD(Activity activity, ViewGroup adContainer, final SplashADListener adListener) {
        new SplashAD(activity, HXADConfig.getAicsKey(activity, HXADConfig.AD_TYPE.SPLASH), adContainer, new SplashADListener() {
            @Override
            public void onNoAD(AdError adError) {
                Log.e(TAG, "onNoAD:");
                if (adListener != null) {
                    adListener.onNoAD(adError);
                }
            }

            @Override
            public void onADPresent(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlShowCallback(true, adLogInfo.getEcpm());
                ADUtils.sendAdLog(adLogInfo);
                Log.e(TAG, "onADPresent...");
                if (adListener != null) {
                    adListener.onADPresent(adLogInfo);
                }
            }

            @Override
            public void onADClicked(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlClickCallback();
                ADUtils.sendAdLog(adLogInfo);
                Log.e(TAG, "onADClicked...");
                if (adListener != null) {
                    adListener.onADClicked(adLogInfo);
                }
            }

            @Override
            public void onADSkip(AdLogInfo adLogInfo) {
                Log.e(TAG, "onADSkip...");
                ADUtils.sendAdLog(adLogInfo);
                if (adListener != null) {
                    adListener.onADSkip(adLogInfo);
                }
            }

            @Override
            public void onADTimeOver(AdLogInfo adLogInfo) {
                Log.e(TAG, "onADTimeOver...");
                ADUtils.sendAdLog(adLogInfo);
                if (adListener != null) {
                    adListener.onADTimeOver(adLogInfo);
                }
            }
        });
    }

    /**
     * 加载插屏广告
     *
     * @param activity activity
     */
    public void showInterstitialAD(final Activity activity) {
        showInterstitialAD(activity, null);
    }

    /**
     * 加载插屏广告
     *
     * @param activity activity
     * @param listener listener
     */
    public void showInterstitialAD(final Activity activity, final OnInterstitialADListener listener) {
        if (iad != null) {
            iad.closeAD();
            iad.destroy();
            iad = null;
        }
        getInterstitialAD(activity, new com.qq.e.o.ads.v2.ads.interstitial.InterstitialADListener() {
            @Override
            public void onADReceive() {
                Log.i(TAG, "InterstitialAD onADReceive");
            }

            @Override
            public void onADPresent(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlShowCallback(false, adLogInfo.getEcpm());
                ADUtils.sendAdLog(adLogInfo);
                Log.i(TAG, "InterstitialAD onADPresent");
            }

            @Override
            public void onADClicked(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlClickCallback();
                ADUtils.sendAdLog(adLogInfo);
                Log.i(TAG, "InterstitialAD onADClicked");
            }

            @Override
            public void onADClosed(AdLogInfo adLogInfo) {
                ADUtils.sendAdLog(adLogInfo);
                Log.i(TAG, "InterstitialAD onADClose");
            }

            @Override
            public void onSuccess(int i) {
                if (listener != null) {
                    listener.onShow();
                }
            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "InterstitialAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
            }
        });
        iad.loadAD();
    }

    private void getInterstitialAD(Activity activity, com.qq.e.o.ads.v2.ads.interstitial.InterstitialADListener adListener) {
        if (iad != null) {
            iad.closeAD();
            iad.destroy();
            iad = null;
        }
        iad = new InterstitialAD(activity, HXADConfig.getAicsKey(activity, HXADConfig.AD_TYPE.INTERSTITIAL), adListener);
    }

    /**
     * 加载原生信息流广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adWidth     广告宽度（dp）
     * @param adHeight    广告高度（dp）
     * @param adStyle     广告样式（参照HXInfoAdStyle类）
     */
    public void showNativeAD(final Activity activity, final ViewGroup adContainer, final int adWidth, final int adHeight, final int adStyle, final boolean isPreload) {
        showNativeAD(activity, adContainer, adWidth, adHeight, adStyle, isPreload, null);
    }

    /**
     * 加载原生信息流广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adWidth     广告宽度（dp）
     * @param adHeight    广告高度（dp）
     * @param adStyle     广告样式（参照HXInfoAdStyle类）
     */
    public void showNativeAD(final Activity activity, final ViewGroup adContainer, final int adWidth, final int adHeight, final int adStyle, final boolean isPreload, final OnNativeVideoADListener onNativeVideoADListener) {
        showNativeAD(activity, adContainer, adWidth, adHeight, adStyle, isPreload, true, onNativeVideoADListener);
    }

    /**
     * 加载原生信息流广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adWidth     广告宽度（dp）
     * @param adHeight    广告高度（dp）
     * @param adStyle     广告样式（参照HXInfoAdStyle类）
     * @param mAutoRender false:回调onSuccess   true:不回调
     */
    public void showNativeAD(final Activity activity, final ViewGroup adContainer, final int adWidth, final int adHeight, final int adStyle, final boolean isPreload, final boolean mAutoRender, final OnNativeVideoADListener onNativeVideoADListener) {
        nad = new NativeAD(activity, HXADConfig.getAicsKey(activity, Integer.parseInt(HXADConfig.AD_TYPE.NATIVE + "" + adStyle)), adContainer, adWidth, adHeight, adStyle, new NativeADListener() {
            @Override
            public void onSuccess(int i, View view) {
                Log.e(TAG, "AdManager Native show Success");
                if (onNativeVideoADListener != null) {
                    onNativeVideoADListener.onShow(view);
                    nad.render();
                }
            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.e(TAG, "adError -> " + adError.getErrorMsg());
                if (onNativeVideoADListener != null) {
                    onNativeVideoADListener.onError();
                }
            }

            @Override
            public void onADPresent(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlShowCallback(false, adLogInfo.getEcpm());
                ADUtils.sendAdLog(adLogInfo);
                if (onNativeVideoADListener != null) {
                    onNativeVideoADListener.onSuccess();
                }
            }

            @Override
            public void onADClicked(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlClickCallback();
                ADUtils.sendAdLog(adLogInfo);
                if (onNativeVideoADListener != null) {
                    onNativeVideoADListener.onClick();
                }
            }

            @Override
            public void onADClosed(AdLogInfo adLogInfo) {
                ADUtils.sendAdLog(adLogInfo);
                Log.w(TAG, "NativeAD onADClosed");
                if (isPreload) {
                    showNativeAD(activity, adContainer, adWidth, adHeight, adStyle, true);
                }
                if (onNativeVideoADListener != null) {
                    onNativeVideoADListener.onClosed();
                }
            }

            @Override
            public void onPreload() {

            }
        }, mAutoRender);
        if (isPreload) {
            nad.preloadAD();
        } else {
            nad.loadAD();
            Log.e(TAG, "开始加载原生广告");
        }
    }

    /**
     * 加载横幅广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adHeight    广告高度（dp）
     */
    public void showBannerAD(Activity activity, ViewGroup adContainer, int adHeight) {
        bad = new BannerAD(activity, HXADConfig.getAicsKey(activity, HXADConfig.AD_TYPE.BANNER), adContainer, adHeight, new BannerADListener() {

            @Override
            public void onSuccess(int i) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "BannerAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
            }

            @Override
            public void onADPresent(AdLogInfo adLogInfo) {
                Log.i(TAG, "BannerAD onADPresent");
                JLUtils.setPlatform(adLogInfo.getSdkType());
                ADUtils.sendAdLog(adLogInfo);
                JLUtils.jlShowCallback(false, adLogInfo.getEcpm());
            }

            @Override
            public void onADClicked(AdLogInfo adLogInfo) {
                Log.i(TAG, "BannerAD onADClicked");
                JLUtils.setPlatform(adLogInfo.getSdkType());
                ADUtils.sendAdLog(adLogInfo);
                JLUtils.jlClickCallback();
            }

            @Override
            public void onADClosed(AdLogInfo adLogInfo) {
                Log.i(TAG, "BannerAD onADClosed");
                ADUtils.sendAdLog(adLogInfo);
                closeBannerAD();
            }
        });
        bad.loadAD();
    }

    /**
     * 关闭横幅广告
     */
    public void closeBannerAD() {
        if (bad != null) {
            bad.destroy();
            bad = null;
        }
    }

    public void loadFullscreenVideoAD(final Activity activity, final boolean isPreload) {
        loadFullscreenVideoAD(activity, isPreload, null);
    }

    /**
     * 预加载或加载全屏视频广告
     *
     * @param activity  activity
     * @param isPreload 是否预加载
     */
    public void loadFullscreenVideoAD(final Activity activity, final boolean isPreload, final OnFullscreenVideoADListener adListener) {
        getFullscreenVideoAD(activity, new FullscreenVideoADListener() {

            @Override
            public void onADLoad() {
                Log.i(TAG, "FullscreenVideoAD onADLoad");
            }

            @Override
            public void onVideoCached(AdLogInfo adLogInfo) {
                Log.i(TAG, "FullscreenVideoAD onVideoCached");
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onADShow() {
                Log.i(TAG, "FullscreenVideoAD onADShow");
            }

            @Override
            public void onADPresent(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlShowCallback(false, adLogInfo.getEcpm());
                ADUtils.sendAdLog(adLogInfo);
                Log.i(TAG, "FullscreenVideoAD onADExpose");
            }

            @Override
            public void onADClicked(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlClickCallback();
                ADUtils.sendAdLog(adLogInfo);
                Log.i(TAG, "FullscreenVideoAD onADClicked");
            }

            @Override
            public void onVideoComplete(AdLogInfo adLogInfo) {
                ADUtils.sendAdLog(adLogInfo);
                Log.i(TAG, "FullscreenVideoAD onVideoComplete");
            }

            @Override
            public void onADClosed(AdLogInfo adLogInfo) {
                Log.i(TAG, "FullscreenVideoAD onADClosed");
                ADUtils.sendAdLog(adLogInfo);
                if (adListener != null) {
                    adListener.onClosed();
                }
                if (fvAdListener != null) {
                    fvAdListener.onClosed();
                }
                if (isPreload) {
                    loadFullscreenVideoAD(activity, true);
                }
            }

            @Override
            public void onSuccess(int i) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "FullscreenVideoAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
                if (adListener != null) {
                    adListener.onFailed(i, adError);
                }
                if (fvAdListener != null) {
                    fvAdListener.onFailed(i, adError);
                }
            }

            @Override
            public void onSkippedVideo(AdLogInfo adLogInfo) {
                Log.i(TAG, "FullscreenVideoAD onSkippedVideo");
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onPreload() {
                Log.i(TAG, "FullscreenVideoAD onPreload");
            }
        });
        if (isPreload) {
            fvAd.preloadAD();
        } else {
            fvAd.loadAD();
        }
    }

    private void getFullscreenVideoAD(Activity activity, FullscreenVideoADListener adListener) {
        fvAd = new FullscreenVideoAD(activity, HXADConfig.getAicsKey(activity, HXADConfig.AD_TYPE.FULLSCREEN_VIDEO), adListener);
    }

    public void loadRewardVideoAD(final Activity activity, final boolean isPreload) {
        loadRewardVideoAD(activity, isPreload, null);
    }

    /**
     * 预加载或加载激励视频广告
     *
     * @param activity  activity
     * @param isPreload 是否预加载
     */
    public void loadRewardVideoAD(final Activity activity, final boolean isPreload, final OnRewardVideoADListener adListener) {
        Log.e(TAG, "准备加载激励视频广告");
        getRewardVideoAD(activity, new RewardVideoADListener() {

            @Override
            public void onADLoad() {
                Log.e(TAG, "RewardVideoAD onADLoad");
            }

            @Override
            public void onVideoCached(AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onVideoCached: " + adLogInfo.toString());
                adLogInfo.setAction(HXADConfig.AD_ACTION.VIDEO_CACHED);
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onADShow() {
                Log.e(TAG, "广告展示，关闭Loading");
                closeLoading("", activity);
            }

            @Override
            public void onADPresent(AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onADPresent: " + adLogInfo.toString());
                adLogInfo.setAction(HXADConfig.AD_ACTION.SHOW);
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlShowCallback(false, adLogInfo.getEcpm());
                ADUtils.sendAdLog(adLogInfo);
                if (adListener != null) {
                    adListener.onShow(adLogInfo.getEcpm());
                }
                if (rvAdListener != null) {
                    rvAdListener.onShow(adLogInfo.getEcpm());
                }
            }

            @Override
            public void onReward(final AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onReward: " + adLogInfo.toString());
                adLogInfo.setAction(HXADConfig.AD_ACTION.VIDEO_REWARD);
                ADUtils.sendAdLog(adLogInfo, new ADUtils.AdLogListener() {
                    @Override
                    public void onSuccess() {
                        Log.e(TAG, "reward onSuccess");
                        isRewardSendSus = true;
                        if (isRewardClosed && !isRewardCallBacked) {
                            Log.e(TAG, "reward onClose by sendAdLog");
                            if (adListener != null && rvAdListener != null) {
                                Log.e(TAG, "reward error");
                            }
                            if (adListener != null) {
                                adListener.onClosed(adLogInfo.getOrderId());
                            }
                            if (rvAdListener != null) {
                                rvAdListener.onClosed(adLogInfo.getOrderId());
                            }
                            isRewardCallBacked = true;
                        }
                    }
                });

                if (adListener != null) {
                    adListener.onReward();
                }
                if (rvAdListener != null) {
                    rvAdListener.onReward();
                }
            }

            @Override
            public void onRewardVerify(RewardInfo rewardInfo, AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onRewardVerify: " + rewardInfo.toString() + adLogInfo.toString());
                UMManager.onEvent(UMManager.Event.IncentiveValidations_num);

                if (rewardInfo.errCode == 0 || rewardInfo.errCode == 20000) {
                    UMManager.onEvent(UMManager.Event.IncentiveValidations_num_correct);
                    adLogInfo.setHasLegal(1);
                    Log.e(TAG, "rewardVerify--true");
                } else {
                    adLogInfo.setHasLegal(0);
                    Log.e(TAG, "rewardVerify--false");
                }
                adLogInfo.setAction(HXADConfig.AD_ACTION.VIDEO_REWARD_VERIFY);
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onADClicked(AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onADClicked: " + adLogInfo.toString());
                adLogInfo.setAction(HXADConfig.AD_ACTION.CLICK);
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlClickCallback();
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onVideoComplete(AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onVideoComplete: " + adLogInfo.toString());
                adLogInfo.setAction(HXADConfig.AD_ACTION.VIDEO_COMPLETE);
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onADClosed(AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onADClosed: " + adLogInfo.toString());
                adLogInfo.setAction(HXADConfig.AD_ACTION.CLOSED);
                ADUtils.sendAdLog(adLogInfo);

                isRewardClosed = true;
                if (isRewardSendSus && !isRewardCallBacked) {
                    Log.e(TAG, "reward onClose by onADClosed");
                    if (adListener != null && rvAdListener != null) {
                        Log.e(TAG, "reward error");
                    }
                    if (adListener != null) {
                        adListener.onClosed(adLogInfo.getOrderId());
                    }
                    if (rvAdListener != null) {
                        rvAdListener.onClosed(adLogInfo.getOrderId());
                    }
                    isRewardCallBacked = true;
                }

                if (isPreload) {
                    loadRewardVideoAD(activity, true);
                }
            }

            @Override
            public void onSuccess(int i) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.e(TAG, "RewardVideoAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
                if (handler != null) {
                    Log.e(TAG, "广告加载失败，关闭Loading");
                    closeLoading("加载广告失败，请切换网络再试", activity);
                }
                if (!isPreload) {
                    if (adListener != null) {
                        adListener.onFailed(i, adError);
                    }
                    if (rvAdListener != null) {
                        rvAdListener.onFailed(i, adError);
                    }
                } else {
                    Log.e(TAG, "广告预加载失败");
                }
            }

            @Override
            public void onSkippedVideo(AdLogInfo adLogInfo) {
                Log.e(TAG, "RewardVideoAD onSkippedVideo");
                adLogInfo.setAction(HXADConfig.AD_ACTION.SKIPPED);
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onPreload() {
                Log.e(TAG, "RewardVideoAD onPreload");
            }

            @Override
            public void onADTimeout() {
                Log.e(TAG, "RewardVideoAD onADTimeout");
                if (handler != null) {
                    Log.e(TAG, "广告加载超时，关闭Loading");
                    closeLoading("加载广告超时，请切换网络再试", activity);
                }
                if (!isPreload) {
                    if (adListener != null) {
                        adListener.onTimeout();
                    }
                    if (rvAdListener != null) {
                        rvAdListener.onTimeout();
                    }
                } else {
                    Log.e(TAG, "广告预加载超时");
                }
            }
        });
        if (isPreload) {
            rvAd.preloadAD();
        } else {
            rvAd.loadAD();
        }
    }

    private void getRewardVideoAD(Activity activity, RewardVideoADListener adListener) {
        rvAd = new RewardVideoAD(activity, HXADConfig.getAicsKey(activity, HXADConfig.AD_TYPE.REWARD_VIDEO), adListener);
    }

    public RewardVideoAD getRvAd() {
        return rvAd;
    }

    //  加载广告
    public void loadNativeAd(final Activity a, int widthDp, int heightDp, int adType, final InterfaceNativeADLoad lis) {
        new NativeAD(a, HXADConfig.getAicsKey(a, Integer.parseInt(HXADConfig.AD_TYPE.NATIVE + "" + adType)), null, widthDp, heightDp, adType, new NativeADListener() {
            @Override
            public void onSuccess(int i, View view) {
                lis.success(view);
            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.e(TAG, "NativeAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
            }

            @Override
            public void onADPresent(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlShowCallback(false, adLogInfo.getEcpm());
                ADUtils.sendAdLog(adLogInfo);
                Log.e(TAG, "NativeAD onADPresent");
            }

            @Override
            public void onADClicked(AdLogInfo adLogInfo) {
                JLUtils.setPlatform(adLogInfo.getSdkType());
                JLUtils.jlClickCallback();
                ADUtils.sendAdLog(adLogInfo);
                Log.e(TAG, "NativeAD onADClicked");
            }

            @Override
            public void onADClosed(AdLogInfo adLogInfo) {
                Log.e(TAG, "NativeAD onADClosed");
                ADUtils.sendAdLog(adLogInfo);
            }

            @Override
            public void onPreload() {
                Log.e(TAG, "NativeAD onPreload");
            }
        }, false).loadAD();
    }

    public interface OnNativeVideoADListener {
        void onSuccess();

        void onClosed();

        void onError();

        void onClick();

        void onShow(View view);
    }

    public interface InterfaceNativeADLoad {
        void success(View v);
    }

    public interface OnFullscreenVideoADListener {
        void onClosed();

        void onFailed(int i, AdError adError);
    }

    public interface OnRewardVideoADListener {
        void onClosed(String orderId);

        void onReward();

        void onShow(double ecpm);

        void onFailed(int i, AdError adError);

        void onTimeout();
    }

    public interface OnInterstitialADListener {
        void onShow();
    }
}